package com.permission.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.permission.entity.SysUserEntity;
import com.permission.entity.SysUserInfoEntity;
import com.permission.service.SysUserInfoService;
import com.permission.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @desc:
 * @author: xhs
 * @date: 2021/8/29 16:47
 * @version: JDK 1.8
 */
@Slf4j
@Service
public class BaseServiceImpl {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserInfoService sysUserInfoService;

    /**
     * 获取当前用户信息
     *
     * @return
     */
    public SysUserEntity getSysUser() {
        String loginId = StpUtil.getLoginIdAsString();
        SysUserEntity userEntity = sysUserService.getBaseMapper().selectOne(new QueryWrapper<SysUserEntity>().lambda()
                .eq(SysUserEntity::getLoginId, loginId));

        return userEntity;
    }


    /**
     * 获取当前用户的详细信息
     *
     * @return
     */
    public SysUserInfoEntity getSysUserInfo() {
        // 从token中获取去登录人账号
        String loginId = StpUtil.getLoginIdAsString();
        // 获取用户信息
        SysUserInfoEntity userInfoEntity = sysUserInfoService.getBaseMapper().selectOne(new QueryWrapper<SysUserInfoEntity>().lambda()
                .eq(SysUserInfoEntity::getLoginId, loginId));
        if (userInfoEntity != null) {
            userInfoEntity.setLoginId(loginId);
            return userInfoEntity;
        } else {
            return null;
        }
    }

    /**
     * 根据账号查询用户信息
     *
     * @param loginId
     * @return
     */
    public SysUserEntity getSysUserEntity(String loginId) {
        LambdaQueryWrapper<SysUserEntity> queryWrapper = new QueryWrapper<SysUserEntity>().lambda().eq(SysUserEntity::getLoginId, loginId);
        return sysUserService.getBaseMapper().selectOne(queryWrapper);
    }
}
